package engine;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import engine.dataobjects.MailItemInstance;
import engine.scheduler.jobs.IMailItemJob;
import engine.scheduler.jobs.IMailItemJobDetail;
import engine.services.IDataResource;



public class Engine {


	@Autowired
	IDataResource<?,?> configResource;
	@Autowired
	IMailItemJob mailItemJob;
	@Autowired
	IMailItemJobDetail mailItemJobDetail;

	Log log = LogFactory.getLog(Engine.class);

	boolean isNow = false;
	
	public void start() {
		log.info("############### reading configuration #############");
		configResource.init();
        log.info("################################# Mailer is running. ######################################");
	}
	
	public void start(boolean isNow) {
		this.isNow = isNow;
		start();
	}
	
	public void initMailItems(List<MailItemInstance<?>> items) {
		this.initMailItems(items, isNow);
	}
	
	public void initMailItems(List<MailItemInstance<?>> items, boolean isNow) {
		
		for (MailItemInstance<?> item : items) {
			mailItemJobDetail.setItem(item);
			mailItemJob.init(mailItemJobDetail, isNow);
		}
	}

	public void setConfigResource(IDataResource<?,?> configResource) {
		this.configResource = configResource;
	}

	public void setMailItemJob(IMailItemJob mailItemJob) {
		this.mailItemJob = mailItemJob;
	}

	public void setMailItemJobDetail(IMailItemJobDetail mailItemJobDetails) {
		this.mailItemJobDetail = mailItemJobDetails;
	}
		
	
}
